package com.hezhi.project.module.studentIc.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hezhi.common.utils.CommonUtil;
import com.hezhi.common.utils.StringUtils;
import com.hezhi.framework.aspectj.LogAspect;
import com.hezhi.framework.config.MyConfig;
import com.hezhi.project.module.studentIc.mapper.StudentIcMapper;
import com.hezhi.project.module.student.domain.Student;
import com.hezhi.project.module.studentIc.domain.Card;
import com.hezhi.project.module.studentIc.domain.StudentIc;
import com.hezhi.project.module.studentIc.service.IStudentIcService;
import com.alibaba.fastjson.JSONObject;
import com.hezhi.common.support.Convert;

/**
 * 学生与IC卡对应 服务层实现
 * 
 * @author hezhi
 * @date 2018-08-06
 */
@Service
public class StudentIcServiceImpl implements IStudentIcService 
{
	@Autowired
	private StudentIcMapper studentIcMapper;
	private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
	@Autowired
	private MyConfig myConfig;

	/**
     * 查询学生与IC卡对应信息
     * 
     * @param studentNumber 学生与IC卡对应ID
     * @return 学生与IC卡对应信息
     */
    @Override
	public StudentIc selectStudentIcByStudentNumber(String studentNumber)
	{
	    return studentIcMapper.selectStudentIcByStudentNumber(studentNumber);
	}
    
    @Override
    public StudentIc selectStudentIcByIcNumber(String icNumber)
    {
    	return studentIcMapper.selectStudentIcByIcNumber(icNumber);
    }
	
	/**
     * 查询学生与IC卡对应列表
     * 
     * @param studentIc 学生与IC卡对应信息
     * @return 学生与IC卡对应集合
     */
	@Override
	public List<StudentIc> selectStudentIcList(StudentIc studentIc)
	{
	    return studentIcMapper.selectStudentIcList(studentIc);
	}
	
    /**
     * 新增学生与IC卡对应
     * 
     * @param studentIc 学生与IC卡对应信息
     * @return 结果
     */
	@Override
	public int insertStudentIc(StudentIc studentIc)
	{
	    return studentIcMapper.insertStudentIc(studentIc);
	}
	
	/**
     * 修改学生与IC卡对应
     * 
     * @param studentIc 学生与IC卡对应信息
     * @return 结果
     */
	@Override
	public int updateStudentIc(StudentIc studentIc)
	{
	    return studentIcMapper.updateStudentIc(studentIc);
	}
	
	/**
     * 保存学生与IC卡对应
     * 
     * @param studentIc 学生与IC卡对应信息
     * @return 结果
     */
	@Override
	public int saveStudentIc(StudentIc studentIc)
	{
	    String studentNumber = studentIc.getStudentNumber();
		int rows = 0;
		if (StringUtils.isNotNull(studentNumber))
        {
		    rows = studentIcMapper.updateStudentIc(studentIc);
		}
		else
        {
		    rows = studentIcMapper.insertStudentIc(studentIc);
		}
		return rows;
	}
	
	/**
     * 删除学生与IC卡对应对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteStudentIcByIds(String ids)
	{
		return studentIcMapper.deleteStudentIcByIds(Convert.toStrArray(ids));
	}

	/**
	 * 查询学生的IC卡号
	 */
	@Override
	public List<Student> selectStudentList(Student student) {
		return studentIcMapper.selectStudentList(student);
	}

	/**
	 * 修改IC卡状态
	 */
	@Override
	public boolean updateStatus(Card card) {
		List<Card> list = new ArrayList<>();
		list.add(card);
		if(studentscard(list)){
			StudentIc studentIc = new StudentIc();
			studentIc.setIcNumber(card.getCardNo());
			studentIc.setStatus(card.getStatus());
			studentIcMapper.updateStudentIc(studentIc);
			return true;
		}else{
			return false;
		}
	}
	

	/**
	 * 下发接口
	 */
	@Override
	public boolean studentscard(List<Card> listCard) {
		
		JSONObject j = CommonUtil.httpRequest(myConfig.getMachineurl()+"/studentscard", "POST", JSONObject.toJSONString(listCard));
		if("success".equals(j.getString("status"))){
			return true;
		}else{
			log.error(JSONObject.toJSONString(j));
			return false;
		}
	}
	
}
